from datetime import datetime
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_finance.service.unitInfo_service import UnitinfoService
from module_finance.entity.vo.unitInfo_vo import DeleteUnitinfoModel, UnitinfoModel, UnitinfoPageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


unitInfoController = APIRouter(prefix='/finance/unitInfo', dependencies=[Depends(LoginService.get_current_user)])


@unitInfoController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('finance:unitInfo:list'))]
)
async def get_finance_unitInfo_list(
    request: Request,
unitInfo_page_query: UnitinfoPageQueryModel = Depends(UnitinfoPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    unitInfo_page_query_result = await UnitinfoService.get_unitInfo_list_services(query_db, unitInfo_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=unitInfo_page_query_result)


@unitInfoController.post('', dependencies=[Depends(CheckUserInterfaceAuth('finance:unitInfo:add'))])
@ValidateFields(validate_model='add_unitInfo')
@Log(title='填报单位主', business_type=BusinessType.INSERT)
async def add_finance_unitInfo(
    request: Request,
    add_unitInfo: UnitinfoModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_unitInfo.create_time = datetime.now()
    add_unitInfo.update_time = datetime.now()
    add_unitInfo_result = await UnitinfoService.add_unitInfo_services(query_db, add_unitInfo)
    logger.info(add_unitInfo_result.message)

    return ResponseUtil.success(msg=add_unitInfo_result.message)


@unitInfoController.put('', dependencies=[Depends(CheckUserInterfaceAuth('finance:unitInfo:edit'))])
@ValidateFields(validate_model='edit_unitInfo')
@Log(title='填报单位主', business_type=BusinessType.UPDATE)
async def edit_finance_unitInfo(
    request: Request,
    edit_unitInfo: UnitinfoModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_unitInfo.update_by = current_user.user.user_name
    edit_unitInfo.update_time = datetime.now()
    edit_unitInfo_result = await UnitinfoService.edit_unitInfo_services(query_db, edit_unitInfo)
    logger.info(edit_unitInfo_result.message)

    return ResponseUtil.success(msg=edit_unitInfo_result.message)


@unitInfoController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('finance:unitInfo:remove'))])
@Log(title='填报单位主', business_type=BusinessType.DELETE)
async def delete_finance_unitInfo(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_unitInfo = DeleteUnitinfoModel(ids=ids)
    delete_unitInfo_result = await UnitinfoService.delete_unitInfo_services(query_db, delete_unitInfo)
    logger.info(delete_unitInfo_result.message)

    return ResponseUtil.success(msg=delete_unitInfo_result.message)


@unitInfoController.get(
    '/{id}', response_model=UnitinfoModel, dependencies=[Depends(CheckUserInterfaceAuth('finance:unitInfo:query'))]
)
async def query_detail_finance_unitInfo(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    unitInfo_detail_result = await UnitinfoService.unitInfo_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=unitInfo_detail_result)


@unitInfoController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('finance:unitInfo:export'))])
@Log(title='填报单位主', business_type=BusinessType.EXPORT)
async def export_finance_unitInfo_list(
    request: Request,
    unitInfo_page_query: UnitinfoPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    unitInfo_query_result = await UnitinfoService.get_unitInfo_list_services(query_db, unitInfo_page_query, is_page=False)
    unitInfo_export_result = await UnitinfoService.export_unitInfo_list_services(unitInfo_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(unitInfo_export_result))

@unitInfoController.put('/changeStatus')
@Log(title='报告审核', business_type=BusinessType.UPDATE)
async def reset_finFilling_main_status(
    request: Request,
    edit_unitInfo: UnitinfoModel,
    query_db: AsyncSession = Depends(get_db)
):

    edit_unitInfo: UnitinfoModel(
        id=edit_unitInfo.id,
        status=edit_unitInfo.status,
        update_time=datetime.now()
    )

    edit_main_result = await UnitinfoService.edit_unitInfo_services(query_db, edit_unitInfo)
    logger.info(edit_main_result.message)

    return ResponseUtil.success(msg=edit_main_result.message)